package com.kingtone.rongji.adapter;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import com.kingtone.rongji.MainActivity;
import com.kingtone.rongji.R;
import com.kingtone.rongji.fragment.SearchFragment;
import com.kingtone.rongji.tool.DensityUtil;

public class LeftCategoryAdapter extends BaseExpandableListAdapter {
	
	private List<String> groupArray;

	private List<List<String>> childArray;
	
	private MainActivity activity;
	
	private LayoutInflater mInflater = null;

	public LeftCategoryAdapter(MainActivity activity, List<String> groupArray,
			List<List<String>> childArray) {
		this.activity = activity;
		this.childArray = childArray;
		this.groupArray = groupArray;
		this.mInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public Object getChild(int groupPosition, int childPosition) {
		return childArray.get(groupPosition).get(childPosition);
	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	public int getChildrenCount(int groupPosition) {
		return childArray.get(groupPosition).size();
	}

	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		View view;
		if (convertView != null) {
			view = convertView;
		} else {
			view = mInflater.inflate(R.layout.fragment_left_category_item, parent, false);
		}
		
		TextView textView = (TextView) view.findViewById(R.id.left_category_item);
		int childLeft = DensityUtil.dip2px(activity, view.getResources().getDimension(R.dimen.left_category_padding_child_left));
		textView.setPadding(childLeft, 0, 0, 0);
		String value = childArray.get(groupPosition).get(childPosition);
		int tempId = 0;
		if(value != null);
		{
			String[] values = value.split("_#_");
			if(values.length >= 2)
			{
				tempId = Integer.valueOf(values[0]);
				textView.setText(values[1]);	
			}
		}
		final int categoryId = tempId;
		view.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View view) {
				activity.onQueryChanged(categoryId, "", true);
			}
		});
		return view;
	}

	// group method stub
	public Object getGroup(int groupPosition) {
		return groupArray.get(groupPosition);
	}

	public int getGroupCount() {
		return groupArray.size();
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		String name = groupArray.get(groupPosition);
		View view;
		if (convertView != null) {
			view = convertView;
		} else {
			view = mInflater.inflate(R.layout.fragment_left_category_item, parent, false);
		}
		TextView textView = (TextView) view.findViewById(R.id.left_category_item);
		textView.setTextAppearance(view.getContext(), R.style.LeftCategoryGroup);
		textView.setText(name);
		return view;
	}

	public boolean hasStableIds() {
		return false;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
}
